﻿using DataBase.Utils;
using GreenXi.Help;
using GreenXi.Help.Plc;
using GreenXi.Help.PLC;
using GreenXi.Job;
using GreenXi.Model;
using GreenXi.Pages.Chart;
using GreenXi.Pages.index;
using GreenXi.Pages.Power;
using GreenXi.Pages.Setting;
using GreenXi.ViewModel;
using Helpers;
using MaterialDesignThemes.Wpf;
using PressSystem_Can.Help.Can;
using S7.Net.Types;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Util;
using static GreenXi.App;
using static GreenXi.ViewModel.LoginViewModel;
using Craft = GreenXi.Pages.Setting.Craft;
using ProductionData = GreenXi.Pages.Power.ProductionData;

namespace GreenXi.Pages.Navig
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private static string SystemNameStr = "";
        private static string CurrentScreenName = "";
        public static StackPanel _StackPanelMain;
        public static MainWindowViewModel Instance { get; set; } = new();

        public MainWindow()
        {
            #region 全部布局
            InitializeComponent();
            _StackPanelMain = StackPanelMain;
            DataContext = Instance;
            //this.MouseDown += MainWindow_MouseDown; 双击任意区域变大缩小
            //获取资源字典
            var resources = System.Windows.Application.Current.Resources;
            ItemMenu item0 = new ItemMenu(resources["mainpage"].ToString(), new PressureCurve(), PackIconKind.ViewDashboard);

            var menuReports = new List<SubItem>();
            var item2 = new ItemMenu("数据查询", menuReports, PackIconKind.FileReport);
            var menuExpenses = new List<SubItem>();
            var item3 = new ItemMenu("工艺配置", menuExpenses, PackIconKind.ShoppingBasket);

            var menuSchedule = new List<SubItem>();
            var item4 = new ItemMenu("系统设置", menuSchedule, PackIconKind.Schedule);

            var menuRegister = new List<SubItem>();
            var item6 = new ItemMenu("用户管理", menuRegister, PackIconKind.Register);

            if (CurrentUser.RoleLevel <= 3) //C权限者可以看的
            {

                //数据查询
                menuReports.Add(new SubItem("电表数据", new ProductionData()));
                menuReports.Add(new SubItem("温湿度表数据", new ProductionData2()));
                menuReports.Add(new SubItem("变压器温度数据", new ProductionData3()));
                menuReports.Add(new SubItem("数据图表", new LocationData()));
                menuReports.Add(new SubItem("电表实时查询", new RealTimeData()));
                menuReports.Add(new SubItem("报警查询", new LogList()));

                //工艺配置
                menuExpenses.Add(new SubItem("电表模块配置", new Craft()));
                menuExpenses.Add(new SubItem("温湿度配置", new Craft2()));
                menuExpenses.Add(new SubItem("变压器温度配置", new Craft3()));
                menuExpenses.Add(new SubItem("虚拟表配置", new Virtual()));
                menuExpenses.Add(new SubItem("区域配置", new Region()));
                menuExpenses.Add(new SubItem("网关配置", new Gateway()));
                //系统设置
                menuSchedule.Add(new SubItem("峰谷时间设置", new Navig.PeakValleyTime()));

                menuSchedule.Add(new SubItem("系统参数设置", new SystemSettingsView()));
                menuSchedule.Add(new SubItem("数据库备份", new Power.BackupInfo()));
           

                menuRegister.Add(new SubItem("账号切换", new SelectUserView()));
                menuRegister.Add(new SubItem("账号管理", new UserView()));
                menuRegister.Add(new SubItem("注销账号"));
            }

            if (CurrentUser.RoleLevel <= 2) //B权限者可以看的
            { }

            if (CurrentUser.RoleLevel <= 1) //A权限者可以看的
            { }
            Menu.Children.Add(new UCMenuItem(item0, this));
            Menu.Children.Add(new UCMenuItem(item2, this));
            Menu.Children.Add(new UCMenuItem(item3, this));
            Menu.Children.Add(new UCMenuItem(item4, this));
            Menu.Children.Add(new UCMenuItem(item6, this));
            #endregion

            SystemNameStr = MyDbContext.Instance.GetValueByKey("SystemName").Value;
            

            var screen = new PressureCurve();

            CurrentScreenName = screen.ToString();

            // 获取现有的 UserControl 实例
            if (screen != null)
            {
                System.Windows.Controls.UserControl newUserControl = (System.Windows.Controls.UserControl)Activator.CreateInstance(screen.GetType()); //根据类型创建新的UserControl
                StackPanelMain.Children.Clear();
                StackPanelMain.Children.Add(newUserControl);
            }
            this.WindowState = WindowState.Maximized;
        }

        internal void SwitchScreen(object sender)
        {
            //停止定时器
            RealTimeData.Instance.StopTimerButton_Click();
            if (GlobalVariables.CurrentMode == 2)
            {
                var screen = ((System.Windows.Controls.UserControl)sender);
                var screenName = screen.ToString();
                if (screen != null && screen.GetType().Name == "PressureCurve")
                {
                    CurrentScreenName = screenName;
                    System.Windows.Controls.UserControl newUserControl = (System.Windows.Controls.UserControl)
                        Activator.CreateInstance(screen.GetType()); //根据类型创建新的UserControl
                    StackPanelMain.Children.Clear();
                    StackPanelMain.Children.Add(newUserControl);
                }
            }
            if (GlobalVariables.CurrentMode == 0)
            {
                var screen = ((System.Windows.Controls.UserControl)sender);
                if (screen == null)
                {
                    return;
                }
                var screenName = screen.ToString();

                List<string> checkNameList =
                    new()
                    {
                       "GreenXi.Pages.Setting.Craft",
                       "GreenXi.Pages.Setting.Craft2",
                       "GreenXi.Pages.Setting.Craft3",
                       "GreenXi.Pages.Setting.Virtual",
                        "GreenXi.Pages.Power.Monitoring: 监控窗口设置",
                        "GreenXi.Pages.Power.DecisionBoxBindingDettings: 监控窗口设置",
                        "GreenXi.Pages.Power.SystemSettingsView",
                        "GreenXi.Pages.Power.BackupInfo",
                          "GreenXi.Pages.Power.UserView",
                            "GreenXi.Pages.Power.BackupInfo",
                            "GreenXi.Pages.Power.SelectUserView"
                    };

                if (screen != null && screenName != CurrentScreenName)
                {

                    if (checkNameList.Contains(screenName) && !GlobalVariables.IsDebugMode)
                    {
                        ChangeUserDialog changeUserDialog = new();
                        if (changeUserDialog.ShowDialog() == true)
                        {
                            var pwd = changeUserDialog.pwd.Password ?? "";
                            LoginViewModel loginViewModel = new();
                            LoginStatus loginStatus = loginViewModel.LoginCheck("admin", pwd);
                            switch (loginStatus)
                            {
                                case LoginStatus.Success:
                                    break;
                                case LoginStatus.Error:
                                    System.Windows.MessageBox.Show("密码错误！");
                                    return;
                                default:
                                    return;
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                    CurrentScreenName = screenName;
                    System.Windows.Controls.UserControl newUserControl = (System.Windows.Controls.UserControl)
                        Activator.CreateInstance(screen.GetType()); //根据类型创建新的UserControl
                    StackPanelMain.Children.Clear();
                    StackPanelMain.Children.Add(newUserControl);
                }
            }

        }

        //关闭软件
        private void ButtonPopUpClose_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Application.Current.Shutdown();
            Environment.Exit(0);
        }

        private void ButtonPopUpLogout_Click(object sender, RoutedEventArgs e)
        {
            ConfirmDialog confirmDialog = new ConfirmDialog("确定要注销登录吗?");
            if (confirmDialog.ShowDialog() == true)
            {
                CurrentUser.Clean();
                new LoginView().Show();
                this.Close();
            }
        }
        private void ButtonPopUpLogout_Click()
        {
            ConfirmDialog confirmDialog = new ConfirmDialog("确定要注销登录吗?");
            if (confirmDialog.ShowDialog() == true)
            {
                CurrentUser.Clean();
                new LoginView().Show();
                this.Close();
            }
        }


        private void ButtonOpenMenu_Click(object sender, RoutedEventArgs e)
        {
            ButtonOpenMenu.Visibility = Visibility.Collapsed;
            ButtonCloseMenu.Visibility = Visibility.Visible;
            LogoMenu.Visibility = Visibility.Visible;
            MenuWidth.Width = new GridLength(200, GridUnitType.Pixel);
         
        }

        private void ButtonCloseMenu_Click(object sender, RoutedEventArgs e)
        {
            ButtonOpenMenu.Visibility = Visibility.Visible;
            ButtonCloseMenu.Visibility = Visibility.Collapsed;
            LogoMenu.Visibility = Visibility.Collapsed;
            MenuWidth.Width = new GridLength(70, GridUnitType.Pixel);
           
        }

        private void GridTitle_MouseDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        private void ButtonPopUpHelp_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.Help.ShowHelp(null, "/Images/W500_NCFK Manual Ver1.0_CN.chm", "xyz_index.html");
        }

        private void MainWindow_MouseDown(object sender, MouseButtonEventArgs e)
        {
            // 双击放大
            //if (e.ClickCount == 1 && e.ChangedButton == MouseButton.Left)
            //{
            //    this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
            //}
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            this.DragMove();
        }

        /// <summary>
        /// 复位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void Reset_Click(object sender, RoutedEventArgs e)
        {


            Logger.Instance.Infomation("用户点击复位");
        
            Reset();
            System.Windows.MessageBox.Show("复位成功");
         
        }


        public static void Reset()
        {
            //程序状态继续
            GlobalVariables.CallPolice = 0;
            GlobalVariables.IsForcedStop = false;
            //清除保护力
            GlobalVariables.IsProtectStop1= false;
            GlobalVariables.IsProtectStop2 = false;
            GlobalVariables.IsLostDisplacementData = false;
        }

        private void MinWindow_MouseDown(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }
    }
}
